package com.dataaccess.dataaccess.config.scheduling;

import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.utils.appkit.FirefightingiotKit;
import com.dataaccess.dataaccess.config.rocketmq.UserProducer;
import com.dataaccess.dataaccess.support.utils.DayUtil;
import com.dataaccess.dataaccess.support.utils.MyLogger;
import com.dataaccess.dataaccess.web.mapper.NormalRecordMapper;
import com.dataaccess.dataaccess.web.service.PostOrGetService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class Scheduling {

    @Resource
    public MyLogger logger;

    @Resource
    NormalRecordMapper normalRecordMapper;
    @Resource
    PostOrGetService postOrGetService;
    @Resource
    UserProducer userProducer;

//    @Resource
//    NormalRecordMapper normalRecordMapper;

//    @Scheduled(cron="* 0/50 * * * ?")
//    public void reportCurrentTime() {
//        List<String> list = normalRecordMapper.getOffLineList();
//        JSONArray jsonArray = new JSONArray();
//        for(String number : list){
//            jsonArray.add(number);
//            System.out.println(number);
//        }
//        logger.info("------------------------------domain: "+"-----------------------------------------");
//        String ip = ConfigKit.getStr("firefightingiot","config");
//        String methodPath = FirefightingiotKit.getStr("offDeviceListUpdate","device");
//        String domain = ip + methodPath;
//        logger.info("------------------------------domain:" + domain + "-----------------------------------------");
//        String xml = HttpRequest.post(domain).body(jsonArray.toJSONString(1)).execute().body();
//        logger.info("------------------------------xml:" + xml + "-----------------------------------------");
//        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
//        logger.info("------------------------------Scheduling:jsonObject:" + jsonObject.toString() + "-----------------------------------------");
//    }

    @Scheduled(cron="0 */5 * * * ?")
    public void updateToOff() {
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("deviceFlag","updateToOff");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            postOrGetService.updateToOff(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
    }
    @Scheduled(cron="0 */5 * * * ?")
    public void updateHEX100ToON() {
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("deviceFlag","updateHEX100ToON");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            postOrGetService.updateHEX100ToON(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
    }
    @Scheduled(cron="0 0 1 * * ?")//每晚1点删除今日表中的无用的记录：昨天的记录
    public void insertLogStatisticsForMonth() throws InterruptedException {
        JSONObject jsonObject = new JSONObject();
        //获取前天的日期
        jsonObject.put("time", DayUtil.getBeforeYestoday());
        deleteTodayLog(jsonObject);
    }

    //删除相关表中的前天以及之前的数据
    public void deleteTodayLog(JSONObject jsonObject){
        //总表
        jsonObject.put("table","alarm_all_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        //电气火灾
        jsonObject.put("table","electricfire_alarm_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","electricfire_all_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","electricfire_normal_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","electricfire_other_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","electricfire_othernormal_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        //灭火器
        jsonObject.put("table","fireExtinguisher_alarm_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","fireExtinguisher_alarm_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","fireExtinguisher_normal_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        //水
        jsonObject.put("table","firewater_alarm_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","firewater_all_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","firewater_normal_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","firewaters_alarm_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","firewaters_normal_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        //燃气
        jsonObject.put("table","gas_alarm_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","gas_alarm_http_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        //烟感
        jsonObject.put("table","smoke_alarm_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","smoke_normal_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        //可视化
        jsonObject.put("table","video_alarm_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        jsonObject.put("table","video_normal_deviceDataChanged");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
        //三江云
        jsonObject.put("table","sanjiangyun_alarm_all_device_info");
        normalRecordMapper.deleteBeforeYesLog(jsonObject);
    }

//    /**
//     * @Author YangYu
//     * @Description 水表平台定时更新设备离线状态
//     * @Date Created in 11:19 2023/11/27
//     * @Param []
//     */
//
//    @Scheduled(cron="0 */1 * * * ?")
//    public void updateToOff() {
//        JSONObject jsonParam = new JSONObject();
//        postOrGetService.updateMeterToOff(jsonParam);
//    }
}
